package com.mycjj.android.activity;

import java.util.HashMap;
import java.util.Map;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.mycjj.android.ProjectDataManage;
import com.mycjj.android.R;
import com.mycjj.android.bean.BaseBean;
import com.mycjj.android.bean.RegisterUserBean;
import com.mycjj.android.http.ProjectHttpRequestImpl;
import com.mycjj.android.http.ProjectHttpRequestInterface;
import com.mycjj.android.util.Preferences;

public class RegisterActivity extends BaseActivity implements android.view.View.OnClickListener{
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);
		mActManager.pushActivity(REGISTER_KEY, this);
		init();
	}
	
	private ImageView mHeaderLeftImageview;
	private Button mHeaderRightButton;
	private TextView mHeaderTitleTextView;
	private void initHeader(){
		mHeaderLeftImageview = (ImageView) findViewById(R.id.header_left_button);
		mHeaderLeftImageview.setImageResource(R.drawable.back);
		mHeaderRightButton = (Button) findViewById(R.id.header_right_button);
		mHeaderRightButton.setVisibility(View.GONE);
		mHeaderTitleTextView = (TextView) findViewById(R.id.header_title_textview);
		mHeaderTitleTextView.setText("注册");
		mHeaderLeftImageview.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				mActManager.popActivity(REGISTER_KEY, RegisterActivity.this);
			}
		});
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK){
			mActManager.popActivity(REGISTER_KEY, RegisterActivity.this);
		}
		return super.onKeyDown(keyCode, event);
	}
	private EditText mEt_tel;
	private EditText mEt_code,mEt_pwd;
	private Button mBtn_send;
	private boolean isRepeatSend = true;// 是否重新发送
	private Button mBtn_register;
	
	private ImageView ck_status;
	private TextView tv_info;
	private boolean isAgree = true;
	private void init(){
		initHeader();
		mEt_tel = (EditText)findViewById(R.id.et_tel);
		mEt_code = (EditText)findViewById(R.id.et_code);
		mEt_pwd = (EditText)findViewById(R.id.et_pwd);
		mBtn_send = (Button)findViewById(R.id.btn_send);
		ck_status = (ImageView)findViewById(R.id.ck_status);
		ck_status.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(isAgree){
					isAgree = false;
					ck_status.setImageResource(R.drawable.ck_unsel);
				}else{
					isAgree = true;
					ck_status.setImageResource(R.drawable.ck_sel);
				}
			}
		});
		tv_info = (TextView)findViewById(R.id.tv_info);
		tv_info.setText(Html.fromHtml("我已经看过并通过 <font color=blue>《车家佳服务协议》</font>"));
		tv_info.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				startActivity(new Intent(RegisterActivity.this,AboutCjjActivity.class));
			}
		});
		mBtn_send.setOnClickListener(this);
		mBtn_register = (Button)findViewById(R.id.btn_register);
		mBtn_register.setOnClickListener(this);
	}
	
	private class ReduceTimeThread extends CountDownTimer {

		public ReduceTimeThread(long millisInFuture, long countDownInterval) {
			super(millisInFuture, countDownInterval);
		}

		@Override
		public void onTick(long millisUntilFinished) {
			isRepeatSend = false;
			mBtn_send.setText(millisUntilFinished / 1000 + "秒等待");
			mBtn_send.setEnabled(false);
		}

		@Override
		public void onFinish() {
			isRepeatSend = true;
			mBtn_send.setText("发送验证码");
			mBtn_send.setEnabled(true);
		}

	}

	@Override
	public void onClick(View v) {
		String phoneNum = mEt_tel.getText().toString();
		if (TextUtils.isEmpty(phoneNum)) {
			Toast.makeText(this, "手机号不能为空!", Toast.LENGTH_LONG).show();
			return;
		}
		switch(v.getId()){
		case R.id.btn_send:
			sendMsgToTel(phoneNum);
			break;
		case R.id.btn_register:
			String code = mEt_code.getText().toString().trim();
			String pwd = mEt_pwd.getText().toString().trim();
			if(TextUtils.isEmpty(code)){
				Toast.makeText(this, "验证码不能为空!", Toast.LENGTH_LONG).show();
				return;
			}else if(TextUtils.isEmpty(pwd)){
				Toast.makeText(this, "密码不能为空!", Toast.LENGTH_LONG).show();
				return;
			}
			
			if(!isAgree){
				Toast.makeText(this, "请阅读并同意《车家佳服务协议》", Toast.LENGTH_LONG).show();
				return;
			}
			
			register(phoneNum,code,pwd);
			break;
		}
	}
	
	private void sendMsgToTel(final String tel) {
		Log.d("mark","sendMsgToTel(String) in success! tel:"+tel);
		Map<String, String> map = new HashMap<String, String>();
		map.put("phone", tel);
		RequestParams params = new RequestParams();
		params.put("client_id", ProjectHttpRequestInterface.client_id_key);
		params.put("mobile", tel);
		params.put("type", "1");
		request = new ProjectHttpRequestImpl(this, true);
		request.requestPhoneMesWithDoneHandler(null, params, new AsyncHttpResponseHandler(){
			@Override
			public void onSuccess(String content) {
				BaseBean<?> bean = ProjectDataManage.getInstance().getBaseBean();
				if (bean != null) {
					if(bean.getCode() == 200){
						mEt_code.setText((String)bean.getData());
						Log.d("mark","repeatSend(String) in success! phoneNum:"+tel);
						if (isRepeatSend) {
							mHandler.sendEmptyMessage(0);
						}
					}else{
						Toast.makeText(RegisterActivity.this, bean.getMsg(), Toast.LENGTH_LONG).show();
					}
				} else {
					Toast.makeText(RegisterActivity.this, "bean is null!!", Toast.LENGTH_LONG).show();
				}
			}
		});
	}
	private void register(final String tel,String code,String pwd){
		request = new ProjectHttpRequestImpl(this, true);
		RequestParams params = new RequestParams();
		params.put("mobile", tel);
		params.put("code", code);
		params.put("password", pwd);
		params.put("client_id", ProjectHttpRequestInterface.client_id_key);
		params.put("device_type", "android");
		request.requestRegUserWithDoneHandler("", params, new AsyncHttpResponseHandler(){
			
			@Override
			public void onSuccess(String content) {
				RegisterUserBean bean = ProjectDataManage.getInstance().getRegisterUserBean();
				if (bean != null ) {
					if(bean.getCode() == 200){
						Preferences.setEditor(RegisterActivity.this, "user_token", bean.getData().getMember_token());
						Intent intent = new Intent(RegisterActivity.this,Register2Activity.class);
						intent.putExtra("tel", tel);
						startActivity(intent);
					}else{
						Toast.makeText(RegisterActivity.this, bean.getMsg(), Toast.LENGTH_LONG).show();
					}
				}else{
					Toast.makeText(RegisterActivity.this, "bean is null!!", Toast.LENGTH_LONG).show();
				}
			}
		});
	}
	
	private ReduceTimeThread reduceTimeTask;


	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			Log.d("mark","handleMessage what:"+msg.what);
			switch (msg.what) {
			case 0:
				reduceTimeTask = new ReduceTimeThread(60000, 1000);
				reduceTimeTask.start();
				break;
			}
		}
	};
}
